package com.wudl.flink.stream;

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import static org.apache.hadoop.yarn.webapp.hamlet.HamletSpec.Media.print;

/**
 * @author ：wudl
 * @date ：Created in 2021-11-28 17:56
 * @description： Flink 的测输出流
 * @modified By：
 * @version: 1.0
 */

public class Flink_SideOutput {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<String> dsStream = env.socketTextStream("192.168.1.130", 9999);
        OutputTag<String> outputTag = new OutputTag<String>("number") {
        };
        SingleOutputStreamOperator<String> process = dsStream.process(new ProcessFunction<String, String>() {
            @Override
            public void processElement(String s, Context ctx, Collector<String> collector) throws Exception {

                // 判断 输入的字符串是否等于 "big"  如果等于big  就输入到  主输出流  否则将抛出异常
                   try {
                       if (s.equals("big")) {
                           collector.collect(s);
                       }else
                       {
                           throw new Exception();
                       }
                   }catch (Exception e)
                   {
                     //  e.printStackTrace();
                       ctx.output(outputTag,s);
                   }
            }
        });

        // 打印主流
        process.print("打印主流 ----->");
        //  打印错误的的流或者是 不符合条件的流
        process.getSideOutput(outputTag).print("侧输出流----->");
        env.execute();

    }

}
